package com.hellogood.yaohuo.client.bussiness.fanchang;

import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;

import com.google.gwt.user.client.rpc.AsyncCallback;
import com.hellogood.yaohuo.client.common.CommonMessages;
import com.hellogood.yaohuo.client.common.QuyuTreeWindow;
import com.hellogood.yaohuo.shared.data.Constants;
import com.hellogood.yaohuo.shared.entity.ThForm;
import com.hellogood.yaohuo.shared.services.FanchangService;
import com.smartgwt.client.data.Record;
import com.smartgwt.client.types.Alignment;
import com.smartgwt.client.types.DateDisplayFormat;
import com.smartgwt.client.types.Positioning;
import com.smartgwt.client.util.SC;
import com.smartgwt.client.widgets.Canvas;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.form.fields.ButtonItem;
import com.smartgwt.client.widgets.form.fields.DateItem;
import com.smartgwt.client.widgets.form.fields.HiddenItem;
import com.smartgwt.client.widgets.form.fields.SelectItem;
import com.smartgwt.client.widgets.form.fields.TextItem;
import com.smartgwt.client.widgets.form.fields.events.ClickEvent;
import com.smartgwt.client.widgets.form.fields.events.ClickHandler;
import com.smartgwt.client.widgets.grid.ListGrid;
import com.smartgwt.client.widgets.grid.ListGridField;
import com.smartgwt.client.widgets.layout.Layout;
import com.smartgwt.client.widgets.layout.VLayout;

public class SearchController {
	private HiddenItem levelItem = null;
	private HiddenItem orgCodeItem = null;
	private TextItem orgItem = null;
	private DateItem startDateItem = null;
	private DateItem endDateItem = null;
	private SelectItem accountSelectItem = null;
	private ButtonItem searchItem = null;

	private ListGrid listGrid = null;
	private DynamicForm form = null;
	private Layout layout = null;

	public SearchController() {
		initView();
	}

	private void initView() {
		levelItem = new HiddenItem("level");
		orgCodeItem = new HiddenItem("orgCode");
		orgItem = new TextItem("org", "组织");
		// orgItem.setCanEdit(false);
		orgItem.setRequired(true);
		orgItem.addClickHandler(new TextItemClickhandler());

		startDateItem = new DateItem("startDate", "开始日期");
		startDateItem.setDateFormatter(DateDisplayFormat.TOJAPANSHORTDATE);
		startDateItem.setUseTextField(true);
		startDateItem.setWidth(100);
		startDateItem.setRequired(true);
		endDateItem = new DateItem("endDate", "结束日期");
		endDateItem.setDateFormatter(DateDisplayFormat.TOJAPANSHORTDATE);
		endDateItem.setUseTextField(true);
		endDateItem.setWidth(100);
		endDateItem.setRequired(true);
		Date date = new Date();
		endDateItem.setValue(date);
		date.setDate(1);
		startDateItem.setValue(date);
		LinkedHashMap<String, String> map = new LinkedHashMap<String, String>();
		map.put(Constants.RUZHANG_CODE_ALL, Constants.RUZHANG_NAME_ALL);
		map.put(Constants.RUZHANG_CODE_NO, Constants.RUZHANG_NAME_NO);
		map.put(Constants.RUZHANG_CODE_YES, Constants.RUZHANG_NAME_YES);
		accountSelectItem = new SelectItem("account", "选择");
		accountSelectItem.setWidth(100);
		accountSelectItem.setValueMap(map);
		accountSelectItem.setValue("all");

		searchItem = new ButtonItem("search", "查询");
		searchItem.setStartRow(false);
		searchItem.addClickHandler(new SearchClickHandler());

		form = new DynamicForm();
		form.setSize("100%", "10%");
		form.setNumCols(14);
		form.setFields(levelItem, orgCodeItem, orgItem, startDateItem, endDateItem, accountSelectItem, searchItem);

		ListGridField codeField = new ListGridField("code", "单号");
		ListGridField orgField = new ListGridField("org", "组织");
		orgField.setWidth("15%");
		ListGridField dateField = new ListGridField("date", "验收日期");
		ListGridField amountField = new ListGridField("amount", "数量");
		amountField.setWidth("5%");
		amountField.setAlign(Alignment.RIGHT);
		ListGridField jPriceField = new ListGridField("jPrice", "进价金额");
		jPriceField.setAlign(Alignment.RIGHT);
		ListGridField priceField = new ListGridField("price", "售价金额");
		priceField.setAlign(Alignment.RIGHT);
		ListGridField zdUserField = new ListGridField("zdUser", "制单人");
		ListGridField zdDateField = new ListGridField("zdDate", "制单日期");
		ListGridField rzDateField = new ListGridField("rzDate", "入账日期");		
		ListGridField remarkField = new ListGridField("remark", "备注");
		ListGridField[] fields = new ListGridField[] { codeField, orgField, dateField, amountField,jPriceField,priceField, zdUserField, zdDateField, rzDateField, remarkField };
		listGrid = new ListGrid();
		listGrid.setFields(fields);
		listGrid.setMargin(2);
		listGrid.setSize("100%", "90%");
		
		layout = new VLayout();
		layout.setSize("100%", "100%");
		layout.setMembers(form, listGrid);
	}

	public Canvas getCanvas() {
		return layout;
	}

	public ListGrid getListGrid(){
		return listGrid;
	}
	
	private class SearchClickHandler implements ClickHandler {

		@Override
		public void onClick(ClickEvent event) {
			if (form.validate()) {
				String orgCode = "";
				if (orgCodeItem.getValue() != null) {
					orgCode = orgCodeItem.getValue().toString();
				}
				String startDate = startDateItem.getDisplayValue().toString();
				String endDate = endDateItem.getDisplayValue().toString();
				int level = Integer.valueOf(levelItem.getValue().toString());
				String rz = accountSelectItem.getValue().toString();
				FanchangService.Util.getInstance().searchFanchangFormByOrgCodeAndDate(orgCode, level, startDate, endDate, rz, new SearchResultAsynCallback());
			}
		}
	}

	private class SearchResultAsynCallback implements AsyncCallback<List<ThForm>> {

		@Override
		public void onFailure(Throwable caught) {
			SC.say(caught.getMessage());
		}

		@Override
		public void onSuccess(List<ThForm> result) {
			if (result != null) {
				if (result.size() == 0) {
					SC.say(CommonMessages.NoData);
				} else {
					Record[] rds = new Record[result.size()];
					for (int i = 0; i < result.size(); i++) {
						Record r = new Record();						
						r.setAttribute("code", result.get(i).getThBillNo());
						r.setAttribute("org", result.get(i).getOrgCode() + "-" + result.get(i).getOrgName());
						r.setAttribute("date", result.get(i).getThDate());
						r.setAttribute("amount", result.get(i).getCounts());					
						r.setAttribute("zdUser", result.get(i).getStaffCode() + "-" + result.get(i).getStaffName());
						r.setAttribute("zdDate", result.get(i).getThDate());
						r.setAttribute("rzDate", result.get(i).getRzDate());						
						r.setAttribute("remark", result.get(i).getRemark());
						rds[i] = r;
					}

					listGrid.setData(rds);
				}
			}
		}
	}

	private class TextItemClickhandler implements ClickHandler {

		@Override
		public void onClick(ClickEvent event) {
			QuyuTreeWindow window = new QuyuTreeWindow(orgItem, levelItem);
			window.setPosition(Positioning.RELATIVE);
			window.setSize("500", "500");
			window.centerInPage();
			window.setTitle("组织选择");
			window.show();
		}
	}	
}
